<?php

namespace App\Http\Controllers\Client;

use App\Services\Tbk\OrderService;
use App\Utils\ApiResponse;
use App\Utils\PriceUtis;
use Illuminate\Http\Request;

class OrderController extends ClientBase
{
    public function getOrderList(Request $request)
    {
        $data = $request->decrypted_data;
        $page = 1;
        if (isset($data->page) && (int) $data->page > 0) {
            $page = $data->page;
        }
        $pageSize = 10;
        if (isset($data->page_size)) {
            if ((int) $data->page_size > 0 && (int) $data->page_size <= 10) {
                $pageSize = $data->page_size;
            }
        }
        $orderService = new OrderService();
        $list = $orderService->getPddOrdersByMemberId($this->currentUser->id, $page, $pageSize);
        $orderList = [];
        foreach ($list as $order) {
            $orderList[] = [
                'id'          => $order->id,
                'create_time' => date("m-d H:i"),
                'status_zh'   => $order->order_status_desc,
                'img'         => $order->goods_thumbnail_url,
                'title'       => $order->goods_name,
                'price'       => handle0(sprintf("%.2f", PriceUtis::toYuan($order->order_amount))),
                'order_sn'    => $order->order_sn,
                'channel_zh'  => '拼多多'
            ];
        }
        ApiResponse::success([
            'order_list' => $orderList,
            'page'       => $page,
            'page_size'  => $pageSize
        ]);
    }

    /**
     * 订单详情
     */
    public function getOrderInfo(Request $request)
    {
        $data = $request->decrypted_data;
        if (!isset($data->id)) {
            ApiResponse::fail("id不能为空");
        }
        $id = (int) $data->id;
        if ($id <= 0) {
            ApiResponse::fail("订单id异常");
        }
        $orderService = new OrderService();
        $orderInfo = $orderService->getPddOrderInfoById($id);
        $return = (object) [];
        if (!empty($orderInfo)) {
            $return->title = $orderInfo->goods_name;
            $return->status_zh = $orderInfo->order_status_desc;
            $return->img = $orderInfo->goods_thumbnail_url;
            $return->old_price = handle0(sprintf("%.2f", PriceUtis::toYuan($orderInfo->goods_price)));
            $return->coupon_price = handle0(sprintf("%.2f",
                PriceUtis::toYuan(bcsub($orderInfo->goods_price, $orderInfo->order_amount, 6))));
            $return->order_create_time = date("Y-m-d H:i", $orderInfo->order_create_time);
            // 扣除平台抽成
            $platPrice = PriceUtis::platPrice($orderInfo->promotion_amount); // 分
            $return->promotion_amount = handle0(sprintf("%.2f",
                PriceUtis::toYuan(bcsub($orderInfo->promotion_amount, $platPrice))));
            $return->order_amount = handle0(sprintf("%.2f", PriceUtis::toYuan($orderInfo->order_amount)));
            $return->order_sn = $orderInfo->order_sn;
            $return->fail_reason = $orderInfo->fail_reason;
            $return->order_status = $orderInfo->order_status;
        }
        ApiResponse::success([
            'info' => $return
        ]);
    }
}
